﻿using PressureTest.Main.Project;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PressureTest
{
    public partial class AddProjectForm : Form
    {
        /// <summary>选择的预设模板
        /// 
        /// </summary>
        public Template template = null;

        /// <summary>模型
        /// 
        /// </summary>
        public Project obj = new Project();

        /// <summary>错误提示
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private void Error(string msg)
        {
            Alert.Error(msg);
        }

        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = NumberTextBox.Text;

            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            int count = list.Count;
            if (count > 0)
            {
                Alert.Error(where["number"] + "工程编号已存在");
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>更新obj
        /// 
        /// </summary>
        /// <returns></returns>
        private Project UpdateObj()
        {
            obj.number = this.NumberTextBox.Text;
            obj.name = this.NameTextBox.Text;
            obj.stake_mark = this.StakeMarkTextBox.Text;
            obj.pipe_specification = this.PipeSpecificationTextBox.Text;
            obj.pipe_length = this.PipeLengthTextBox.Text;
            obj.medium = this.MediumTextBox.Text;
            obj.design_pressure = this.template.design_pressure.ToString();
            obj.pipe_nature = this.PipeNatureComboBox.Text;
            obj.test_type = this.TestTypeComboBox.Text;
            obj.company = this.CompanyTextBox.Text;
            obj.manometer_precision = this.ManometerPrecisionComboBox.Text;
            obj.manometer_range = this.ManometerRangeComboBox.Text;
            obj.template_id = this.template.id;
            return obj;
        }
        public AddProjectForm()
        {
            InitializeComponent();
        }

        private void AddProjectForm_Load(object sender, EventArgs e)
        {
           
        }

        /// <summary>刷新模板信息
        /// 
        /// </summary>
        public void freshTemplate() 
        {
            if (template.number == 0)
            {
                Alert.Error("请重新选择模板");
                return;
            }

            this.SelectTemplateButton.Text = template.design_pressure.ToString();
        }

        private void SelectTemplateButton_Click(object sender, EventArgs e)
        {
            SelectTemplateForm frm = new SelectTemplateForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        /// <summary>确定
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (!NumberTextBox.Text.IsValid())
            {
                Error("请输入工程编号");
                return;
            }
            if (!NameTextBox.Text.IsValid())
            {
                Error("请输入工程名称");
                return;
            }
            if (!StakeMarkTextBox.Text.IsValid())
            {
                Error("请输入桩号");
                return;
            }
            if (!PipeSpecificationTextBox.Text.IsValid())
            {
                Error("请输入管材规格");
                return;
            }
            if (!PipeLengthTextBox.Text.IsValid())
            {
                Error("请输入管道长度");
                return;
            }
            if (!MediumTextBox.Text.IsValid())
            {
                Error("请输入试压介质");
                return;
            }

            if (template == null)
            {
                Error("请选择试压模板");
                return;
            }
            if (!PipeNatureComboBox.Text.IsValid())
            {
                Error("请选择管道性质");
                return;
            }
            if (!TestTypeComboBox.Text.IsValid())
            {
                Error("请选择试压类型");
                return;
            }
            if (!CompanyTextBox.Text.IsValid())
            {
                Error("请输入施工单位");
                return;
            }
            if (!ManometerPrecisionComboBox.Text.IsValid())
            {
                Error("请选择压力表精度");
                return;
            }
            if (!ManometerRangeComboBox.Text.IsValid())
            {
                Error("请选择压力表量程");
                return;
            }

            UpdateObj();
            List<Project> list = new List<Project>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                Alert.Info("创建工程成功");
                //OperationSuccess();
            }
            else
            {
                Alert.Error("创建工程失败");
            }
        }
    }
}
